import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';

Reducer<LoginState> buildReducer() {
  return asReducer(
    <Object, Reducer<LoginState>>{
      LoginAction.refreshUI:_resetTelphoneAndCode,
      LoginAction.sendSMSCodeCowndown: _onSendSMSCodeCowndown,
      LoginAction.login: _onLogin,
      LoginAction.showOrHideResetButton:_showOrHideResetButton,
      LoginAction.resetInfo:_resetTelphoneAndCode,
    },
  );
}

LoginState _onSendSMSCodeCowndown(LoginState state, Action action) {
  final LoginState newState = state.clone();
  newState.currCountTime = state.currCountTime;
  return newState;
}

LoginState _onLogin(LoginState state, Action action) {
  final LoginState newState = state.clone();
  newState.currCountTime = state.currCountTime;
  return newState;
}

LoginState _showOrHideResetButton(LoginState state, Action action) {
  final LoginState newState = state.clone();
  newState.showClearButton = state.textTelephoneView.text.isEmpty;
  return newState;
}

LoginState _resetTelphoneAndCode(LoginState state, Action action) {
  final LoginState newState = state.clone();
  newState.showClearButton = true;
  newState.textTelephoneView.text = '';
  newState.textVerifyCodeView.text = '';
  newState.currCountTime = 0;
  if (action.payload['phone']!=null){
    newState.textTelephoneView.text = action.payload['phone'];
  }
  if (action.payload['pwd']!=null){
    newState.textVerifyCodeView.text = action.payload['pwd'];
  }
  return newState;
}